package com.scalar.chiptrack.calendar.actions;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.calendar.dao.CalendarDAO;
import com.scalar.chiptrack.calendar.forms.CalendarForm;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.users.User;

/**
 * Created by Veeru.
 * Date: Jul 29, 2004
 * Time: 2:23:41 PM
 */

public class CalendarAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/calendar.do";
    public ActionForward execute( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response ) throws Exception
    {

        HashMap errors  =  new HashMap();
        HashMap info    =  new HashMap();
        String action   = request.getParameter( "action" );

        CalendarForm subform = ( CalendarForm ) form;
        HttpSession  session = request.getSession ();

        User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );

        if ( user == null )
        {
            session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString () );
            return ( mapping.findForward ( "login" ) );
        }
        if( action == null )
        {
                subform.setYear( Calendar.getInstance().get(Calendar.YEAR)+"" );
                CalendarDAO.loadCalendar( subform );
                request.setAttribute( "PeriodList", subform.getPeriodList() );
                if( subform.getPeriodList().size() > 0 )
                {
                    request.setAttribute( "showPeriods", "TRUE" );
                }
        }
        else if( "loadPeriods".equalsIgnoreCase( action ) )
        {
            if ( subform.getYear() == null || "".equals( subform.getYear() ) || "".equals( subform.getYear() ) || "-2".equals( subform.getYear() ) )
            {
                request.setAttribute( "PeriodList", new ArrayList() );
                subform.setFirstDay("");
                subform.setLastDay("");
                subform.setWwStartDate("");
            }
            else
            {
                CalendarDAO.loadCalendar( subform );
                request.setAttribute( "PeriodList", subform.getPeriodList() );
                if( subform.getPeriodList().size() > 0 )
                {
                    request.setAttribute( "showPeriods", "TRUE" );
                }
            }
        }
        else if( "calculate".equalsIgnoreCase( action ) )
        {
                CalendarDAO.calculateFiscalPeriods( subform );
                request.setAttribute( "PeriodList", subform.getPeriodList() );
                if( subform.getPeriodList().size() > 0 )
                {
                    request.setAttribute( "showPeriods", "TRUE" );
                }
        }
        else if( "workWeek".equalsIgnoreCase( action ) )
        {
            subform.setWorkWeek( CalendarDAO.getWorkWeek( subform.getWorkWeekDate() ) );
            request.setAttribute( "PeriodList", subform.getPeriodList() );
            if( subform.getPeriodList().size() > 0 )
            {
                request.setAttribute( "showPeriods", "TRUE" );
            }
            request.setAttribute( "showWorkWeek", "TRUE" );
        }
        else if( "save".equalsIgnoreCase( action ) )
        {
            DBTransaction dbTransaction = null;
            boolean success = false;
            String quarterNo[]   = request.getParameterValues( "quarterNo" );
            String quarterName[] = request.getParameterValues( "quarterID" );
            String fromDate[]    = request.getParameterValues( "fromdt" );
            String toDate[]      = request.getParameterValues( "todt" );

            try
            {
                dbTransaction = DBTransaction.getInstance ();
                dbTransaction.begin ();
                CalendarDAO.saveCalendar(dbTransaction, subform, quarterNo, quarterName, fromDate, toDate);
                success = true;
            }
            catch ( ScalarException se )
            {
                errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_IN_SAVING_COMPANY_CALENDAR );
                errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
                session.setAttribute ( SessionKeys.ERROR_REPORT_SUBJECT, " Error: Could Not Save Company Calendar" );
                session.setAttribute ( SessionKeys.ERROR_REPORT, se );
            }
            finally
            {
                if( success == true)
                {
                    dbTransaction.commit();
                    info.put( "Company Calendar ", "Saved Successfully" );
                    request.setAttribute( SessionKeys.INFO_MESSAGES, info );
                }
                else
                {
                    dbTransaction.rollback();
                }
            }
        }
        request.setAttribute( "FinancialYears", CalendarDAO.loadYears() );
        request.setAttribute( "YearsMap", CalendarDAO.loadYearsMap() );
        if( "-2".equals( subform.getYear() ) )
        {
            request.setAttribute( "showYear", "TRUE");
        }
        else
        {
            subform.setNewYear("");
        }
        if ( !errors.isEmpty () )
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        return mapping.findForward( "success" );
    }
}
